package com.Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @create 2022-03-2022/3/18-21:43
 */
public class GamePanel extends JPanel implements KeyListener, ActionListener {
    //画板

    private int length;
    private int[] snake_x= new int[600];
    private int[] snake_y= new int[600];
    private String head = "";//R,L,U,D
    private String tail = "";//R,L,U,D
    //游戏状态
    private boolean isStart= false;
    private boolean isDeath=false;
    //
    Food food= new Food();
    Timer timer=new Timer(100,this);


    public GamePanel(){
        init();
        this.setFocusable(true);//获取键盘
        this.addKeyListener(this);//开启监听
        timer.start();//动起来
    }

    //初始化小蛇
    public void init(){
        length=3;
        snake_x[0]=100;snake_y[0]=100;//头
        snake_x[1]=90;snake_y[1]=100;//身子
        snake_x[2]=80;snake_y[2]=100;//身子
        snake_x[3]=70;snake_y[3]=100;//身子
        snake_x[4]=60;snake_y[4]=100;//尾巴
        head="R";
        tail="R";
    }

    //画画
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);//清屏
        //设置背景颜色
        this.setBackground(Color.WHITE);
        StaticData.map.paintIcon(this,g,0,0);
        //游戏区域
        g.fillRect(0,0,600,600);

        //画🐍头
        if(head.equals("R")){
            StaticData.head_right.paintIcon(this,g,snake_x[0],snake_y[0]);
        }else if(head.equals("L")){
            StaticData.head_left.paintIcon(this,g,snake_x[0],snake_y[0]);
        }else if(head.equals("U")){
            StaticData.head_up.paintIcon(this,g,snake_x[0],snake_y[0]);
        }else if(head.equals("D")) {
            StaticData.head_down.paintIcon(this,g,snake_x[0],snake_y[0]);
        }
        //画🐍身
        for (int i=1;i<length;i++){
            StaticData.body.paintIcon(this,g,snake_x[i],snake_y[i]);

        }
        //画🐍尾
        if(tail.equals("R")){
            StaticData.tail_right.paintIcon(this,g,snake_x[length],snake_y[length]);
        }else if(tail.equals("L")){
            StaticData.tail_left.paintIcon(this,g,snake_x[length],snake_y[length]);
        }else if(tail.equals("U")){
            StaticData.tail_up.paintIcon(this,g,snake_x[length],snake_y[length]);
        }else if (tail.equals("D")){
            StaticData.tail_down.paintIcon(this,g,snake_x[length],snake_y[length]);
        }
        //画食物
        StaticData.food.paintIcon(this,g,food.getX(),food.getY());

        //游戏提示
        if(isStart==false){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,20));
            g.drawString("Press space to start or pause",200,100);
        }
        if(isDeath==true){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,20));
            g.drawString("Vegetable,chicken;new start? ",200,120);
        }


    }

    //按下->动作
    @Override
    public void keyPressed(KeyEvent e) {
        int kyecode= e.getKeyCode();
        //按空格开始或暂停
        if (kyecode==KeyEvent.VK_SPACE){
            if (isDeath==true){
                isDeath=false;
                init();
            }else {
                isStart=!isStart;
            }
            repaint();//刷新界面；
        }
        //小蛇方向
        if (kyecode==KeyEvent.VK_LEFT ||kyecode==KeyEvent.VK_A) head = tail = "L";
        if (kyecode==KeyEvent.VK_RIGHT ||kyecode==KeyEvent.VK_D) head = tail = "R";
        if (kyecode==KeyEvent.VK_UP||kyecode==KeyEvent.VK_W) head = tail = "U";
        if (kyecode==KeyEvent.VK_DOWN ||kyecode==KeyEvent.VK_S) head = tail = "D";
    }
    //时间监听,定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart==true&&isDeath==false){
            //身子移动
            for (int i=length;i>0;i--){
                snake_x[i]=snake_x[i-1];
                snake_y[i]=snake_y[i-1];
            }
            if (head.equals("R")){
                //头尾移动
                snake_x[0]=snake_x[0]+10;
                if(snake_x[0]>600){
                    snake_x[0]=0;
                }
            }
            if (head.equals("L")){
                snake_x[0]=snake_x[0]-10;

                if(snake_x[0]<0){
                    snake_x[0]=600;
                }
            }
            if (head.equals("U")){
                snake_y[0]=snake_y[0]-10;

                if(snake_y[0]<0){
                    snake_y[0]=600;
                }
            }
            if (head.equals("D")){
                snake_y[0]=snake_y[0]+10;

                if(snake_y[0]>600){
                    snake_y[0]=0;
                }
            }
            //吃食物
            if(snake_x[0]==food.getX()&&snake_y[0]==food.getY()){
                length++;
                //生成新食物；
                food = new Food();
            }
            //失败判断
            for (int i=1;i<length;i++){
                if (snake_x[0]==snake_x[i]&&snake_y[0]==snake_y[i]){
                    isDeath=true;
                }
            }
            //刷新界面
            repaint();
        }
        timer.start();
    }

    //弹起->动作
    @Override
    public void keyReleased(KeyEvent e) {}

    //按下，—>等->弹起->动作
    @Override
    public void keyTyped(KeyEvent e) {}


}
